/**
 * Created by Administrator on 2017/10/10.
 */
angular.module('module.set')
    .controller('SetPayController', SetPayController);
SetPayController.$inject = ['$localStorage', '$state', 'FileUpload', 'SetService'];
function SetPayController($localStorage, $state, FileUpload, SetService) {

    var vm = this;
    vm.img = 'images/up-qrcode.png';

    if (!$localStorage[environmental + "shopid"]) {
        $localStorage.$reset();
        $state.go('login');
    }

    vm.shopPayList = []; // 初始化支付收款码列表为空
    var shopid = $localStorage[environmental + "shopid"];
    vm.uploadImages = uploadImages;
    vm.makeCode = makeCode;
    vm.saveShopPay = saveShopPay;
    vm.cancelShopPay = cancelShopPay;
    vm.editChange = editChange;
    vm.removeShopPay = removeShopPay;
    vm.editShopPay = editShopPay;
    vm.sureEdit = sureEdit;
    vm.shopPayInfoChange = shopPayInfoChange;
    vm.isEdit = false;
    vm.inuptFile = true; // 默认为上传文件状态
    var shopPayInfo = {};
    active();

    function active() {

        // 获取店铺支付配置
        SetService.getShopPay(shopid, function (shopPayList) {
            vm.shopPayList = shopPayList;
            console.log(shopPayList);
        }, function (error) {
            console.log(error);
        });
    }

    // 页面文字转二维码显示
    function makeCode(shopPay) {
        var qrCodeBox = document.getElementById("qrcode" + shopPay.createtime);
        if (!qrCodeBox) {
            setTimeout(function () {
                makeCode(shopPay);
            }, 10);
            return;
        }
        $(qrCodeBox).find("img").remove();
        $(qrCodeBox).find("canvas").remove();
        var codeCanvas = new QRCode(qrCodeBox, {
            width: 200,//设置宽高
            height: 200
        });
        codeCanvas.makeCode(shopPay.qr);
    }

    // 上传图片解析二维码
    function uploadImages(file, shoppay) {
        if (!file) {
            return;
        }
        vm.progressPercentage = 0;
        var fr = new FileReader();
        fr.readAsDataURL(file);
        FileUpload.uploadFileToUrl(file, false, progressTest).then(function (data) {
            if (data.data.faddress) {

                // 通过图片地址解析二维码
                FileUpload.qrDecode(data.data.faddress, function (data) {
                    shopPayInfo = initShopPay(data.content);
                    if (shoppay) {
                        shopPayInfo.createtime = shoppay.createtime;
                        shoppay.info = shopPayInfo.info;
                        shoppay.qr = shopPayInfo.qr;
                        shoppay.edit = true;
                        vm.shopPay = shoppay;
                    } else {
                        shopPayInfo.createtime = new Date().getTime();
                        vm.shopPayList.push(shopPayInfo);
                        vm.inuptFile = false;
                    }
                }, function (error) {
                    alert("图片解析二维码失败，请确认图片是否正确");
                    console.log(error)
                })
            }
        }, function (error) {
            console.log(error);
        });
    }

    // 进度条
    function progressTest(evt) {
        vm.progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
        $(".progress-bar").css({width: vm.progressPercentage + "%"});
    }

    // 保存当前收款码
    function saveShopPay() {
        if (!shopPayInfo.info || shopPayInfo.info == "") {
            alert("请输入收款码类型");
            return;
        }
        SetService.setShopPay(shopPayInfo, function (shopPay) {
            shopPayInfo.createtime = shopPay.createtime;
            shopPayInfo.edit = false;
            vm.inuptFile = true;
            alert("添加收款码成功");
        }, function () {
            alert("添加收款码失败");
        });
    }

    // 取消当前收款码
    function cancelShopPay() {
        vm.shopPayList.splice(vm.shopPayList.length - 1, 1);
        vm.inuptFile = true;
    }

    // 改变编辑状态
    function editChange() {
        vm.isEdit = !vm.isEdit;
    }

    // 删除支付二维码
    function removeShopPay(shoppay, $index) {
        var r = confirm("确认删除该收款码？");
        if (r == false) {
            return;
        }
        SetService.removeShopPay(shoppay, function () {
            vm.shopPayList.splice($index, 1);
            vm.isEdit = false;
            alert("删除成功");
        }, function () {
            alert("删除失败");
        });
    }

    // 上传收款二维码
    function editShopPay(file, shoppay) {
        uploadImages(file, shoppay);
    }

    // 更新收款码信息
    function upDataShopPay(shopPayInfo, shoppay) {
        SetService.updateShopPay(shopPayInfo, function (shopPayData) {
            shoppay.info = shopPayData.info;
            shoppay.qr = shopPayData.qr;
            shoppay.edit = false;
            shoppay.change = false;
            makeCode(shoppay);
            //alert("修改成功");
        }, function () {
            alert("修改失败");
        });
    }

    // 编辑完成
    function sureEdit() {

        // 未做操作
        if (!vm.shopPay && !vm.infoChange) {
            editChange();
            vm.isEdit = false;
            return;
        }
        if (vm.shopPay) {
            if (!vm.shopPay.info || vm.shopPay.info == "") {
                alert("请输入收款码类型");
                return;
            }
        }

        // 修改收款码信息流程
        if (vm.infoChange) {
            for (var i = 0; i < vm.shopPayList.length; i++) {
                if (vm.shopPayList[i].change) {
                    if (!vm.shopPayList[i].info || vm.shopPayList[i].info == "") {
                        alert("请输入收款码类型");
                        return;
                    }
                    upDataShopPay(vm.shopPayList[i], vm.shopPayList[i])
                }
            }
            editChange();
            vm.isEdit = false;
        }
    }

    // 收款码输入改变监听
    function shopPayInfoChange(shopPay) {
        shopPay.change = true;
        vm.infoChange = true; // 记录是否有做输入操作
    }

    // 根据二维码链接判断收款码类型初始化信息
    function initShopPay(content) {
        var info = "", edit = false, infoInit = {};
        if (content.indexOf("wxp") > -1) {
            info = "微信";
        } else if (content.indexOf("ALIPAY") > -1 || content.indexOf("alipay") > -1) {
            info = "支付宝";
        } else {
            edit = true;
        }
        infoInit = {
            "shopid": shopid,
            "info": info,
            "qr": content,
            "edit": edit
        }; // 初始化收款信息对象
        return infoInit;
    }
}